package com.example.demo.practice;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Criteria;
//import android.location.Location;
//import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.Button;


import com.example.demo.practice.ui.WebMapView;
import com.ydhcasm.casmibeacon.control.BLLocationListener;
import com.ydhcasm.casmibeacon.control.LocationListener;
import com.ydhcasm.casmibeacon.control.Control;
import com.ydhcasm.casmibeacon.control.IControl;



public class Table1 extends Fragment implements LocationListener {
    private WebMapView webMapView;
    private boolean isLocate;
    private LocationManager locationManager;
    private LocationListener locationListener;
    private IControl control;
    private float X;
    private float Y;

//    private Location location_blue;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            webMapView.loadHtmlMethod("addLocation(" + X + "," + Y + ")");
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tab01, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initMapView();
    }

    private void initMapView() {
        WebView webView = getActivity().findViewById(R.id.mapView);
        webMapView = new WebMapView(webView);
        webMapView.loadMapHtml("html/other/index.html");
        isLocate = false;
        //蓝牙定位设置
        control = Control.builder(getActivity())
                .setCyclePeriod(2000)
                //.setBLMode(true)//设置成true 返回经纬度
                .setLocationListener(this)
                .build();
//        Button locate = getActivity().findViewById(R.id.locate);
//        locate.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                if (isLocate) {
//
//                    if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
//                        return;
//                    }
//                    isLocate = false;
//                    locationManager.removeUpdates(locationListener);
//                } else {
//                    isLocate = true;
//                    locate();
//                }
//
//            }
//        });
        //蓝牙监听
        Button locate_blueTooth = getActivity().findViewById(R.id.blueTooth);
        locate_blueTooth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isLocate){
                    control.stop();
                    isLocate=false;
                    webMapView.loadHtmlMethod("stopLocation()");
                }else {
                    control.start();
                    isLocate=true;
                }
            }
        });
    }

    /**
     * 开始定位
     */
    private void locate() {
        locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        //ACCURACY_HIGH/ACCURACY_LOW精度选择
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        //高度
        criteria.setAltitudeRequired(true);
        //方位信息
        criteria.setBearingRequired(true);
        //是否允许付费
        criteria.setCostAllowed(true);
        //对电量的要求
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        //速度
        criteria.setSpeedRequired(true);
        String locationProvider = locationManager.getBestProvider(criteria, true);
        if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            Log.e("定位", "没有定位权限");
        } else {
            if (locationProvider != null) {
//                Location location = locationManager.getLastKnownLocation(locationProvider);
//                addMarker(location);
            }
//            locationListener = new locateListener();
//            locationManager.requestLocationUpdates(locationProvider, 1000, 1, locationListener);
        }

    }

    @Override
    public void onLocation(float v, float v1) {
        X = v;
        Y = v1;
        handler.sendEmptyMessage(0);
    }


    /**
     * 位置监听
     */
//    public class locateListener implements LocationListener {
//
//        @Override
//        public void onLocationChanged(Location location) {
//            addMarker(location);
//        }
//
//        @Override
//        public void onStatusChanged(String s, int i, Bundle bundle) {
//
//        }
//
//        @Override
//        public void onProviderEnabled(String s) {
//
//        }
//
//        @Override
//        public void onProviderDisabled(String s) {
//
//        }
//    }

    /**
     * 添加地图定位图标
     * @param location
     */
//    private void addMarker(Location location) {
//        if (location!=null){
//            Log.e("定位","非空");
//            webMapView.loadHtmlMethod("addLocateMarker(" + location.getLatitude() + "," + location.getLongitude() + ")");
//        }
//        else {
//            Log.e("定位","空");
//            return;
//        }
//    }

}
